//Keys are now Working and boxes move on key press

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.awt.*;
@SuppressWarnings("serial")
public class Tetris
{
// constants
public static int x = 0;
public static int y = 0;
private static final int CELL_SIZE = 30; // in pixels
private static final int NCOLS = 10;
private static final int NROWS = 20;
private static final int INITIAL_DELAY = 500; // milliseconds
private static final int DELAY_HALF_LIFE = 100; // in pieces played
// misc fields: initialized by createAndShowGUI
private static JLabel scoreText;
private static JPanel gamePanel;
private static javax.swing.Timer timer;
// start the game
private static void start() {
timer.setDelay(INITIAL_DELAY);
timer.start();
TetrisBoard gameboard = new TetrisBoard(NROWS,NCOLS);
}
// stop the game
private static void stop() {
timer.stop();
}
// timer callback
private static void tick() {
timer.setDelay(INITIAL_DELAY); // change to speed up game over time
}
// keyboard callback
private static void keyPress(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode==e.VK_LEFT)    //checks that if user presses 1 then whether an action is performed or not
{
moveLeft();
}

if(keyCode==e.VK_RIGHT)    //checks that if user presses 1 then whether an action is performed or not
{
moveRight();
}

if(keyCode==e.VK_DOWN)    //checks that if user presses 1 then whether an action is performed or not
{
moveDown();
}

if(keyCode==e.VK_R)
{

rotate();
}




}


public static void moveLeft()    //Method to move Left
{
System.out.println("Left");
createAndShowGUI();
if(x==0)  //checking for walls
{
}
else
{
x=x-50;
}
}

public static void moveRight()  //Method to move Right
{

System.out.println("Right");
createAndShowGUI();
if(x==250)
{
}
else
{
x=x+50;
}
}

public static void moveDown()  //method to move Downwards. Should be different from Left and right methods bcz now tetris block should be accelerating
{
System.out.println("Down");
createAndShowGUI();
if(y==550)
{
}
else
{
y=y+50;
}
}

public static void rotate()      //method to rotate the tetris block
{

System.out.println("Rotate");

}


public Graphics calller(Graphics g)
{
return g;	
	
}


private static void draw(Graphics g,int x,int y) {
	
	g.drawRect(x,y,50,50);
	
}
private static void createAndShowGUI() {
// Create widgets.
JFrame frame = new JFrame("Tetris");
gamePanel = new JPanel() {           // Anonymous Inner class
public void paintComponent(Graphics g) {

draw(g,x,y);
draw(g,x,y);
//draw(g);
//g.drawRect(0,0,50,50);  //will draw a new rectangle 




}
};
JLabel scoreLabel = new JLabel("Score:");
scoreText = new JLabel();
// Register callbacks.
frame.addKeyListener(
new KeyAdapter() {
public void keyPressed(KeyEvent e) {
keyPress(e);
}
}
);

// Arrange widgets.
Box gameBox = Box.createHorizontalBox();
gameBox.setBorder(new BevelBorder(BevelBorder.LOWERED));
gameBox.add(gamePanel);
Box scoreBox = Box.createHorizontalBox();
scoreBox.setBorder(new EmptyBorder(5,5,5,5));
scoreBox.add(scoreLabel);
scoreBox.add(Box.createHorizontalStrut(10));
scoreBox.add(scoreText);
scoreBox.add(Box.createHorizontalGlue());
Box mainBox = Box.createVerticalBox();
mainBox.setBorder(new EmptyBorder(10,10,10,10));
mainBox.add(gameBox);
mainBox.add(scoreBox);
// Finalize geometry.
gamePanel.setPreferredSize(new Dimension(
NCOLS * CELL_SIZE, NROWS * CELL_SIZE));
frame.add(mainBox);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Initialize timer.
timer = new javax.swing.Timer(0,
new ActionListener() {
public void actionPerformed(ActionEvent e) { tick(); }
});
// Start the game.
start();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
